package framework.ui;

import org.client.otdr.OTDRID;
import org.openems.entity.Entity;
import org.openems.entity.Key;
import org.openems.entity.ListEntity;
import org.openems.entity.MarkerEntity;
import framework.DomainService;
import framework.EntityStoreListener;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Point2D;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;

public abstract class EmsMarker extends Pane {
	abstract protected Node getContent(MarkerEntity markerEntity);
	abstract protected void markerChanged(MarkerEntity markerEntity);
	
	protected DomainService getService() {
		return service;
	}

	private static final String BALLOONICONIMG = "/framework/ui/Button-Talk-Balloon-icon48.png";
	private double xoffset;
	private double yoffset;
	private DomainService service;
	private Label balloonIcon;
	private Image image;
	
	public EmsMarker(final Key key, final IChart chart, final DomainService service) {
		this.service = service;
		MarkerEntity markerEntity = (MarkerEntity)service.getEntity(key);
		balloonIcon = new Label();
		balloonIcon.setOpacity(0.5);
		
		image = new Image(getClass().getResource(getBalloonImageFile()).toExternalForm());
		this.setPrefSize(image.getWidth(), image.getHeight());
		balloonIcon.setGraphic(new ImageView(image));
		
		
        xoffset = image.getWidth() / 2.0;
        yoffset = image.getHeight() + 0;
        
        this.setOnMouseDragged(new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent arg0) {
				Point2D pt = EmsMarker.this.localToParent(arg0.getX(), arg0.getY());
				MarkerEntity markerEntity = (MarkerEntity)service.getEntity(key);
				markerEntity.setValue(chart.getXValue(pt.getX()));
				
				service.putEntity(markerEntity);
			}
        });

        service.addEntityStoreListener(new EntityStoreListener() {
			@Override
			public void valueChanged(Entity entity) {
				MarkerEntity markerEntity = (MarkerEntity)entity;
				
				markerChanged(markerEntity);
				
				updateMarker(markerEntity, chart, xoffset, yoffset);
			}
        	
        }, key);
        
        Label lineLabel = new Label();
        lineLabel.setPrefWidth(balloonIcon.getWidth());
        lineLabel.setLayoutX(xoffset);
        this.getChildren().add(lineLabel);
        this.getChildren().add(balloonIcon);
        this.getChildren().add(getContent(markerEntity));
        
        this.balloonIcon.setOnMousePressed(new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent arg0) {
				if (arg0.isSecondaryButtonDown()) {
					Button button = new Button("Remove");
					getChildren().add(button);
					button.setOnAction(new EventHandler<ActionEvent>() {
						@Override
						public void handle(ActionEvent arg0) {
							ListEntity list = (ListEntity)service.getEntity(OTDRID.get().MARKERLIST);
							list.getKeys().remove(key);
							service.putEntity(list);
						}
					});
				}
			}
        });
	}


	protected String getBalloonImageFile() {
		return BALLOONICONIMG;
	}

	private void updateMarker(MarkerEntity markerEntity, IChart chart, double xoffset, double yoffset) {
		double x = chart.getXLocation(markerEntity.getValue());
		double y = chart.getYLocation(markerEntity.getYvalue());
		
		this.setLayoutX(x - xoffset);
		this.setLayoutY(y - yoffset);
	}
	
	protected double getBalloonWidth() {
		return this.image.getWidth();
	}
	
	protected double getBalloonHeight() {
		return this.image.getHeight();
	}
}
